package thing

import (
	"fmt"
	"github.com/gin-gonic/gin"
	logic2 "iot-base/auth/api/logic"
	"iot-base/auth/common"
	Log "iot-base/auth/logger"
	types2 "iot-base/auth/types"
	"iot-base/common/ctrltype"
	"strings"
)

// GetPresets 获取预设列表.
func GetPresets(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqGetPresets
	if err := c.ShouldBind(&req); err != nil {
		fmt.Println(err)
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	if strings.Contains(req.ThingId, "#") {
		ipcName := strings.Split(req.ThingId, "#")
		req.ThingId = ipcName[0]
	}
	list, err := logic2.NewPresetLogic().GetPresets(req.ThingId)
	if err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.Fail, "")
		return
	}
	resp.Success(gin.H{"list": list})
}

// RemovePreset 移除预设.
func RemovePreset(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqRemovePreset
	if err := c.ShouldBind(&req); err != nil {
		fmt.Println(err)
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	if strings.Contains(req.ThingId, "#") {
		ipcName := strings.Split(req.ThingId, "#")
		req.ThingId = ipcName[0]
	}
	params := map[string]interface{}{
		"ProfileToken": req.ProfileToken,
		"PresetToken":  req.PresetToken,
	}
	var cmd ctrltype.CtrlCmd
	cmd.ID = types2.RemovePreset
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	if result != nil && err == nil {
		resp.Success(gin.H{})
	} else {
		Log.Log.Errorf("[%s] [%v] GotoPreset err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.Fail, "")
	}
}

// SetPreset 设置预设.
func SetPreset(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqSetPreset
	if err := c.ShouldBind(&req); err != nil {
		fmt.Println(err)
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	if strings.Contains(req.ThingId, "#") {
		ipcName := strings.Split(req.ThingId, "#")
		req.ThingId = ipcName[0]
	}
	params := map[string]interface{}{
		"ProfileToken": req.ProfileToken,
		"PresetName":   req.PresetName,
		"PresetToken":  req.PresetToken,
	}
	var cmd ctrltype.CtrlCmd
	cmd.ID = types2.SetPreset
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	if result != nil && err == nil {
		err = logic2.NewPresetLogic().SetPreset(req)
		if err != nil {
			Log.Log.Errorf("[%s] [%v] logic.NewPresetLogic().SetPreset err:=%v", requestId, uid, err)
		}
		resp.Success(gin.H{})
	} else {
		Log.Log.Errorf("[%s] [%v] GotoPreset err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.Fail, "")
	}
	return
}

// GotoPreset 转到预设.
func GotoPreset(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqGotoPreset
	if err := c.ShouldBind(&req); err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	if strings.Contains(req.ThingId, "#") {
		ipcName := strings.Split(req.ThingId, "#")
		req.ThingId = ipcName[0]
	}
	params := map[string]interface{}{
		"ProfileToken": req.ProfileToken,
		"PresetToken":  req.PresetToken,
	}
	var cmd ctrltype.CtrlCmd
	cmd.ID = types2.GotoPreset
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	if result != nil && err == nil {
		resp.Success(result)
	} else {
		Log.Log.Errorf("[%s] [%v] GotoPreset err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.Fail, "")
	}
	return
}
